
#ifndef  _XSENSOR_H_
#define  _XSENSOR_H_

#ifdef _cplusplus
extern "c"{
#endif

#include "Sdr.h"
#include "EventData.h"
#include "SensorEventType.h"
#include "Crb.h"

#define EVENT_MESSAGE_REV 0x04

typedef struct _SensorEvent
{
	uint8 	m_reserved;	
	
	uint8 	m_isDeassert:1;	
	uint8 	m_isValid:1;	
	uint8 	m_reserved1:2;	
	uint8 	m_offset:4;	
	
	uint8 	m_Threshold;	
}SensorEvent;

typedef int (*SensorFun)(void* pSensor);
typedef void (*GetEventDataFun)(void* pSensorObj);

typedef struct _Operate
{
	#ifdef BIG_EDIAN
	uint8 	m_EventMsgDisable:1;	//[7] - 0b = disable all Event Messages from this sensor (optional)
	uint8 	m_ScanningDisable:1;	//[6] - 0b = disable scanning on this sensor (optional)
	uint8 	m_Feature:2;			//[5:4] - 00b = do not change individual enables
	uint8 	m_Reserved:4;			//[3:0] - reserved
	#else
	uint8 	m_Reserved:4;			//[3:0] - reserved
	uint8 	m_Feature:2;			//[5:4] - 00b = do not change individual enables
	uint8 	m_ScanningDisable:1;	//[6] - 0b = disable scanning on this sensor (optional)
	uint8 	m_EventMsgDisable:1;	//[7] - 0b = disable all Event Messages from this sensor (optional)
	#endif
}Operate;

//
enum
{
	 RAW_VALUE = 1
	,REPORTED_VALUE
	,ALL_DATA
};

typedef int (*StateChangedFun)(void* pCallBackObj, void* pSensor, uint8 newState, uint8 olderState);
typedef struct _Sensor
{
	uint8 	m_State:3;	
	uint8 	m_isReadingUnavailable:3;	
	uint8 	m_isEventMsgEnable:1;	
	uint8 	m_isScanEnable:1;	
	uint8 	m_isValid:1;	
	uint8 	m_isShare:1;	
	uint8 	m_isAlert:1;	
	
	uint8 		m_SensorId;
	//uint8 		m_SensorType;
	
	uint8 		m_DefaultValue;			//Reading value from sensor.
	uint8 		m_RawValue;			//Reading value from sensor.
	uint8 		m_ReceiverValue;		//Value of Reporting to CM
	uint8 		m_HandledValue;		//Value of Handling
	
	EventData 	m_EventData;		//RawValue convert to EventData
	EventData 	m_ReceiverEventData;		//EventData of Reporting to CM
	
	uint16 	m_ReadingEvent;

	uint8 		m_IdString[MAX_STR_LEN];
	
	SdrHeader* 	m_pRecodHeader;
	Crb*		m_pCrb;

	//Operation
	SensorFun 	Reset;			
	SensorFun 	Scan;				
	GetEventDataFun	GetEventData;
	void* m_pCallBackObj;
}Sensor;

void Sensor_Init(Sensor* pSensor
	, SdrHeader* 		pCompactSensorRecord
	, uint8 			shareId	//0 = not share record,  > 0 = share reccode
	, SensorFun			Scan
	, GetEventDataFun	GetEventData
	, Crb*				pCrb
	, void* 			pCallBackObj
	, uint8 			defaultValue
	);

Bool Sensor_IsEventMsgEnable(Sensor* pSensor);
void Sensor_GetEventData(Sensor* pSensor);
Bool Sensor_Check(Sensor* pSensor, StateChangedFun	Changed, void* pCallBackObj);
int Sensor_SendEventMsg(Sensor* pSensor);
Sensor* Sensor_New(Sensor sensor[], uint8 sensorCount);
int Sensor_Reset(Sensor* pSensor);


#ifdef _cplusplus
}
#endif

#endif             

